<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar">
      <van-button class="search-btn"
                  slot="title"
                  type="info"
                  size="small"
                  round
                  @click="$router.push('/search')"
                  icon="search">搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道标签 -->
    <!-- swipeable 开启手势滑动 -->
    <!-- animated 开启动画 -->
    <van-tabs v-model="active"
              animated
              swipeable>
      <van-tab v-for="item in userChannels"
               :key="item.id"
               :title="item.name">
        <!-- 频道列表组件 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 右侧插槽 -->
      <!-- 占位空白 -->
      <div slot="nav-right"
           class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div slot="nav-right"
           @click="isChannelEditShow=true"
           class="hamburger-btn">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道标签 -->

    <!-- 频道编辑 -->
    <van-popup v-model="isChannelEditShow"
               position="bottom"
               closeable
               close-icon="close"
               close-icon-position="top-left"
               :style="{ height: '100%' }">

      <channel-edit :active-index="active"
                    :user-channels="userChannels"
                    @close="isChannelEditShow=false"
                    @update-index="active=$event" />
      <!-- $event 表示 子组件传递出来的数据 -->
    </van-popup>
    <!-- /频道编辑 -->

  </div>
</template>

<script>
import { getUserChannels } from '@/api/channel'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapGetters } from 'vuex'
import { getItem } from '@/utils/storage'
  export default {
    components: {
      ArticleList,
        ChannelEdit
    },
    name:'Home',
    data() {
      return {
        active: 0,  // tab激活下标
        userChannels:[],  // 用户频道数据
        isChannelEditShow:false // 编辑频道控制
      }
    },
    computed: {
      ...mapGetters(['haslogin'])
    },
    created () {
      this.loadUserChannels()
      console.log('home被创建了');
    },
    methods: {
      // 获取用户频道数据
      async loadUserChannels() {
        // 重构获取数据的逻辑
        if(this.haslogin){
          //登录了 获取线上该用户的数据
          try {
            let {data:res } = await getUserChannels()  // 带token
            this.userChannels = res.data.channels
          } catch (error) {
            console.dir(error);
            this.$toast.fail('获取用户频道数据失败')
          }
        }else{
          // 没登录，判断本地存储是否有
          if(getItem('TOUTIAO_CHANNEL')){
            // 存在就用这个
            this.userChannels = getItem('TOUTIAO_CHANNEL')
          }else{
            // 不存在就用线上默认的（默认推荐）
            try {
              let {data:res } = await getUserChannels()  // 不带token
              this.userChannels = res.data.channels
            } catch (error) {
              console.dir(error);
              this.$toast.fail('获取用户频道数据失败')
            }
          }
        }
         
      }
    },
  }
</script>

<style lang="less" scoped>
.home-container {
  // 深度选择器问题
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  /deep/ .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
  }
  /deep/ .van-tabs__line {
    width: 32px;
    height: 6px;
    background-color: #3296fa;
    bottom: 40px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>